#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
    int fd = open("love.txt", O_RDONLY);
    if (fd == -1){
        perror("open failed");
        exit(EXIT_FAILURE);
    }

    int fd2 = open ("love2.txt", O_CREAT | O_WRONLY | O_APPEND, 0664);
    if (fd2 == -1){
        perror("open failed");
        exit(EXIT_FAILURE);
    }

    char buffer[1024];
    ssize_t read_bytes;
    while ((read_bytes = read(fd, buffer, sizeof(buffer))) > 0)
    {
        write(STDOUT_FILENO, buffer, read_bytes);
        write(fd2, buffer, read_bytes);
    }
    if (read_bytes == -1)
    {
        perror("read failed");
        close(fd);
        close(fd2);
        exit(EXIT_FAILURE);
    }

    close(fd);
    close(fd2);
    return 0;
    
    
}